using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_TinhTrangDanhSach : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            loadDropDownList();
            loadData();
        }
    }
    private void loadDropDownList()
    {
        tbTinhTrangCollection ttCollection = tbTinhTrangBUS.GetAll();
        ddl_matinhtrang.DataValueField = tbTinhTrangColumns.MaTT.ToString();
        ddl_matinhtrang.DataSource = ttCollection;
        ddl_matinhtrang.DataBind();
        ddl_matinhtrang.Items.Insert(0, new ListItem("All", "0"));

    }
    private void loadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_tentinhtrang.Text != "")
        {
            c.add(Condition.AND, tbTinhTrangColumns.TenTinhTrang, Condition.LIKE, txt_tentinhtrang.Text);
        }
        if (ddl_matinhtrang.SelectedValue != "0")
        {
            c.add(Condition.AND, tbTinhTrangColumns.MaTT, Condition.EQUAL, ddl_matinhtrang.SelectedValue);
        }

        int total = 0;
        tbTinhTrangCollection ttCollection =
        tbTinhTrangBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
        tbTinhTrangColumns.MaTT.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = ttCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
        {
            Pager.Visible = false;
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            tbTinhTrang tinhtrang = (tbTinhTrang)e.Item.DataItem;
            Literal lit_MaTT = (Literal)e.Item.FindControl("lit_MaTT");
            Literal lit_TenTinhTrang = (Literal)e.Item.FindControl("lit_TenTinhTrang");

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/Admin/TinhTrangUpdate.aspx?id=" + tinhtrang.MaTT.ToString();

        }
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        loadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        loadData();
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int iD = int.Parse(hf_id.Value);
                tbTinhTrang tinhtrang = tbTinhTrangBUS.GetByID(iD);
                tbTinhTrangBUS.Delete(iD);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}